package com.medvalley.tcm.controller.admin.service;

import com.google.common.collect.Lists;
import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.controller.admin.vo.inputvo.ReportEditInputVo;
import com.medvalley.tcm.controller.admin.vo.inputvo.SearchReportInputVo;
import com.medvalley.tcm.controller.admin.vo.outputvo.PageDataOutputVo;
import com.medvalley.tcm.controller.admin.vo.outputvo.ReportOutputVo;
import com.medvalley.tcm.dao.SickInfoDao;
import com.medvalley.tcm.dao.SickUserDao;
import com.medvalley.tcm.dao.SickYangShengDao;
import com.medvalley.tcm.domain.SickAndUser;
import com.medvalley.tcm.domain.SickInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linsong
 * @date 2019/6/19 15:22
 **/
@Service
public class BackReportService {
    @Autowired
    private SickInfoDao sickInfoDao;
    @Autowired
    private SickUserDao sickAndUserDao;

    @Autowired
    private SickYangShengDao sickYangShengDao;

    public PageDataOutputVo<ReportOutputVo> searchReport(SearchReportInputVo searchReportInputVo) {
        PageDataOutputVo<ReportOutputVo> reportPageData = new PageDataOutputVo();
        int pageNo = searchReportInputVo.getPageNo();
        int pageSize = searchReportInputVo.getPageSize();
        List<ReportOutputVo> reportOutputVoList = Lists.newArrayList();
        reportPageData.setPageNo(pageNo);
        reportPageData.setPageSize(pageSize);
        pageNo = pageNo - 1 < 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.DESC, "id");
        Specification<SickInfo> specification = new Specification<SickInfo>() {

            @Override
            public Predicate toPredicate(Root<SickInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                // 创建时间-  开始
                if (!StringUtils.isEmpty(searchReportInputVo.getBeginTime())) {
                    list.add(criteriaBuilder.greaterThan(root.get("createTime").as(String.class), searchReportInputVo.getBeginTime()));
                }
                // 创建时间-  结束
                if (!StringUtils.isEmpty(searchReportInputVo.getEndTime())) {
                    list.add(criteriaBuilder.lessThan(root.get("createTime").as(String.class), searchReportInputVo.getEndTime()));
                }
                String barcode = searchReportInputVo.getBarcode();
                if (!StringUtils.isEmpty(barcode)) {
                    list.add(criteriaBuilder.equal(root.get("barCode").as(String.class), barcode));
                }
                String phone = searchReportInputVo.getPhone();
                if (!StringUtils.isEmpty(phone)) {
                    list.add(criteriaBuilder.equal(root.get("patientPhone").as(String.class), phone));
                }
                String org = searchReportInputVo.getOrg();
                if (!StringUtils.isEmpty(org)) {
                    list.add(criteriaBuilder.like(root.get("diagnosticMechanismName").as(String.class), org + "%"));
                }
                String name = searchReportInputVo.getName();
                if (!StringUtils.isEmpty(name)) {
                    list.add(criteriaBuilder.like(root.get("patientName").as(String.class), name + "%"));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
        Page<SickInfo> pageData = sickInfoDao.findAll(specification, pageable);
        if (pageData.hasContent()) {
            List<SickInfo> sickInfoList = pageData.getContent();
            for (SickInfo sickInfo : sickInfoList) {
                ReportOutputVo reportOutputVo = new ReportOutputVo();
                reportOutputVo.setId(sickInfo.getId());
                reportOutputVo.setBarcode(sickInfo.getBarCode());
                reportOutputVo.setReportPhone(sickInfo.getPatientPhone());
                reportOutputVo.setDiagnosticMechanismName(sickInfo.getDiagnosticMechanismName());
                reportOutputVo.setName(sickInfo.getPatientName());
                reportOutputVo.setGender(sickInfo.getPatientSex());
                reportOutputVo.setTongueId(sickInfo.getTongueID());
                reportOutputVo.setPulseId(sickInfo.getPulseID());
                reportOutputVo.setPhysiqueType(sickInfo.getPhysiqueType());
                reportOutputVoList.add(reportOutputVo);
            }
        }
        reportPageData.setTotalElement(pageData.getTotalElements());
        reportPageData.setTotalPageCount(pageData.getTotalPages());
        reportPageData.setPageData(reportOutputVoList);

        return reportPageData;
    }

    @Transactional
    public void deleteReport(long id) {
        sickInfoDao.findById(id).orElseThrow(() -> new BusinessException("无此报告"));
        List<SickAndUser> sickAndUserList = sickAndUserDao.findAllBySickId(id);
        if (!CollectionUtils.isEmpty(sickAndUserList)) {
            sickAndUserDao.deleteAll(sickAndUserList);
        }
        sickYangShengDao.deleteBySickId(id);
        sickInfoDao.deleteById(id);
    }

    public void editReport(ReportEditInputVo editInputVo) {
        long id = editInputVo.getId();
        SickInfo sickInfo = sickInfoDao.findById(id).orElseThrow(() -> new BusinessException("无此报告"));
        sickInfo.setBarCode(editInputVo.getBarCode());
        sickInfo.setPatientName(editInputVo.getName());
        sickInfo.setPatientPhone(editInputVo.getPhone());
        sickInfo.setPatientAge(editInputVo.getAge());
        sickInfo.setPatientSex(editInputVo.getGender());
        sickInfo.setDiagnosticMechanismName(editInputVo.getDiagnosticMechanismName());
        sickInfoDao.save(sickInfo);
    }

}
